package com.bxs.demo;

import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class SocketClient {
    public void send() throws IOException {
        System.out.println("send");
        Socket socket = new Socket("127.0.0.1", 9999);
        InputStream inputStream = socket.getInputStream();
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write("client msg".getBytes(StandardCharsets.UTF_8));
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        char buf[] = new char[1024];
        int len = 0;
        StringBuffer sb = new StringBuffer();
        do{
            len = bufferedReader.read(buf, 0, 1024);
            sb.append(buf, 0, len);
        }while (bufferedReader.ready());
        System.out.println("read: " + sb.toString());
        socket.close();
    }

    public static void main(String[] args) throws IOException {
        SocketClient client = new SocketClient();
        Scanner scanner = new Scanner(System.in);
        String cmd;
        while(true){
            String next = scanner.next();
            System.out.println("input: " + next);
            client.send();
        }
    }
}
